// pages/application/application.js
import common from "../../utils/getcurrentTime.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeUrl:'',
    qualificationUrl:'',
    storeCloudUrl:'',
    qualificationCloudUrl:'',
    position:{},
    tags:['传统美食','名酒名茶','新鲜水果','腌腊干货','绿色蔬菜','粮油作物','美味湖鲜','特色鲜肉','其它产品']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow:function(options) {
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      account:userInfo.account
    })
  },
  /**
   * 上传店铺头像
   */
  addavatarUrl(){
    wx.chooseImage({
      count: 1,
    }).then(res=>{
      this.setData({
        storeUrl:res.tempFilePaths[0]
      })
    })
  },
  
  /**
   * 定位
   */
  chooseLocation(){
    wx.chooseLocation({
      latitude: 0,
    }).then(res=>{
      this.setData({
        position:res
      })
    })
  },

  /**
   *上传营业执照 
   */
  addQualification(){
    wx.chooseImage({
      count: 0,
    }).then(res=>{
      this.setData({
        qualificationUrl:res.tempFilePaths[0]
      })
    })
  },

  /**
   * 获取申请内容
   */
  async formSubmit(e){
    let details = e.detail.value
    let {storeName,checkbox} = details

    let storeUrl = this.data.storeUrl
    let phoneNumber = this.data.account
    let position = this.data.position
    let qualificationUrl = this.data.qualificationUrl


    const p = new Promise((resolve,reject)=>{
      let flag = this.checkValidity(storeUrl,qualificationUrl,storeName,phoneNumber,position,checkbox)
      if(flag){
        resolve("成功数据")
      }else{
        reject("访问失败！！！")
      }
      
    })

    try {
      let result = await p
      this.uploadFile(storeUrl,qualificationUrl,storeName,phoneNumber,position,checkbox)
    }catch(e){
      console.log(e)
    }

  },

  /**
   * 验证输入有效性
   */
  checkValidity(storeUrl,qualificationUrl,storeName,position,checkbox){
    if(storeUrl.length == 0){
      wx.showToast({
        title: '店铺头像未添加',
        icon:'error'
      })
      return false
    }else if(storeName.length == 0){
      wx.showToast({
        title: '店铺名称未添加',
        icon:'error'
      })
      return false
    }else if(Object.getOwnPropertyNames(position).length == 0){
      wx.showToast({
        title: '店铺位置未添加',
        icon:'error'
      })
      return false
    }else if(checkbox.length == 0){
      wx.showToast({
        title: '销售种类未选择',
        icon:'error'
      })
      return false
    }else if(qualificationUrl.length == 0){
      wx.showToast({
        title: '营业执照未添加',
        icon:'error'
      })
      return false
    }else{
      wx.showLoading({
        title:'上传中'
      })
      return true
    }
  },

  /**
   * 上传店铺申请信息到云端
   */
  async uploadFile(storeUrl,qualificationUrl,storeName,phoneNumber,position,checkbox){
    const p = new Promise((resolve,reject)=>{
      let urls = {}
      wx.cloud.uploadFile({
        cloudPath: 'storeInfo/' + phoneNumber +'storeIcon.png',
        filePath: storeUrl, // 文件路径
      }).then(res=>{
          urls.storeCloudUrl = res.fileID

          wx.cloud.uploadFile({
            cloudPath: 'storeInfo/' + phoneNumber+'qualificationIcon.png',
            filePath: qualificationUrl, // 文件路径
          }).then(res=>{
            urls.qualificationCloudUrl = res.fileID
            resolve(urls)
          }).catch(error=>{
            wx.showToast({
              title: '图片上传失败',
              icon:'error'
            })
          })

      }).catch(error=>{
        wx.showToast({
          title: '图片上传失败',
          icon:'error'
        })
      })
    })

    try{
      let result = await p
      let phoneNumber = this.data.account
      let {storeCloudUrl,qualificationCloudUrl} = result
      let date = common.setDate(Date.now(),"Y-m-d H:i:s")

      wx.cloud.callFunction({
        name:'addStoreInfo',
        data:{
          storeUrl:storeCloudUrl,
          storeName:storeName,
          phoneNumber:phoneNumber,
          position:position,
          tags:checkbox,
          qualificationUrl:qualificationCloudUrl,
          date:date,
          checkingstate:0
        }
      }).then(()=>{
        wx.hideLoading({
        })

        wx.showModal({
          content:'申请信息已上传成功，是否查看申请状态？',
          cancelText:'返回',
          confirmText:'确认',
        }).then(res=>{
          if(res.confirm){
            wx.redirectTo({
              url: '/pages/applicationInfo/applicationInfo',
            })
          }else if(res.cancel){
            wx.reLaunch({
              url: '/pages/mine/mine',
            })
          }
        })
      }).catch(()=>{
        wx.showToast({
          title: '上传失败',
          icon:'error'
        })
      })
    }catch(e){

    }
  }
})